<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Post_model extends CI_Model {
	function add($data) {
		$this->db->trans_start();
		$this->db->insert('post', $data);
		$insert_id = $this->db->insert_id();
		$this->db->trans_complete();
		log_message('debug', $this->db->last_query());
		
		return $insert_id;
	}
	
	function get_by_id($post_id) {
		$this->db->where('id', $post_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$query = $this->db->get('post');
		log_message('debug', $this->db->last_query());
		
		$count = $this->db->count_all_results();
		return $count == 1 ? $query->row_array() : NULL;
	}
	
	function exists_id($post_id) {
		$this->db->where('id', $post_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$query = $this->db->get('post');
		log_message('debug', $this->db->last_query());
		
		return $this->db->count_all_results() >= 1;
	}
	
	function get_simple_list($page, $page_size) {
		$this->db->order_by('create_time', 'DESC');
		$query = $this->db->get('vw_simple_post', $page_size, ($page - 1) * $page_size);
		log_message('debug', $this->db->last_query());
		
		return $query->result_array();
	}
	
	function get_simple_list_all_count() {
		$this->db->from('vw_simple_post');
		$all_count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $all_count;
	}
	
	function get_simple_list_by_category($category_id, $page, $page_size) {
		$this->db->where('category_id', $category_id);
		$this->db->order_by('create_time', 'DESC');
		$query = $this->db->get('vw_simple_post', $page_size, ($page - 1) * $page_size);
		log_message('debug', $this->db->last_query());
		
		return $query->result_array();
	}
	
	function get_simple_list_by_category_all_count($category_id) {
		$this->db->where('category_id', $category_id);
		$this->db->from('vw_simple_post');
		$all_count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $all_count;
	}

	function is_voted($post_id, $user_id) {
		$this->db->where('post_id', $post_id);
		$this->db->where('user_id', $user_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$this->db->from('user_vote_post');
		$count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $count >= 1;
	}
	
	function vote($post_id, $user_id, $choice_num) {
		$this->db->trans_start();
		$this->db->insert('user_vote_post', array(
			'user_id' => $user_id,
			'post_id' => $post_id,
			'status' => DB_STATUS_NORMAL,
			'create_time' => date('Y-m-d H:i:s')
		));
		log_message('debug', $this->db->last_query());
		$this->db->where('id', $post_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$field_name = "c{$choice_num}_count";
		$this->db->set("{$field_name}", "{$field_name}+1", FALSE);
		$this->db->set('vote_count', 'vote_count+1', FALSE);
		$this->db->set('last_vote_time', date('Y-m-d H:i:s'));
		$this->db->update('post');
		log_message('debug', $this->db->last_query());
		$affected_rows = $this->db->affected_rows();
		$this->db->trans_complete();
		
		return $affected_rows;
	}
	
	function belongs_to_user($post_id, $user_id) {
		if ($post_id <=0 || $user_id <= 0) return FALSE;
		$this->db->where('id', $post_id);
		$this->db->where('user_id', $user_id);
		$this->db->where('status', DB_STATUS_NORMAL);
		$this->db->from('post');
		$count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $count >= 1;
	}
	
	function get_abnormal_list($page, $page_size) {
		$this->db->where('vote_count >', 10);
		$this->db->where('c1_count <', 'c2_count', FALSE);
		$this->db->order_by('create_time', 'DESC');
		$query = $this->db->get('vw_simple_post', $page_size, ($page - 1) * $page_size);
		log_message('debug', $this->db->last_query());
		
		return $query->result_array();
	}
	
	function get_abnormal_list_all_count() {
		$this->db->where('vote_count >', 10);
		$this->db->where('c1_count <', 'c2_count', FALSE);
		$this->db->from('vw_simple_post');
		$all_count = $this->db->count_all_results();
		log_message('debug', $this->db->last_query());
		
		return $all_count;
	}
	
	function get_top_by_time_range($start_time, $end_time, $num) {
		$this->db->where('create_time >=', $start_time);
		$this->db->where('create_time <=', $end_time);
		$this->db->order_by('vote_count', 'DESC');
		$this->db->order_by('create_time', 'DESC');
		$this->db->limit($num);
		$query = $this->db->get('vw_simple_post');
		log_message('debug', $this->db->last_query());
		
		return $query->result_array();
	}
}